package com.tonyodev.fetchmigrator.helpers;

import ohos.data.resultset.ResultSet;

import com.tonyodev.fetch2.EnqueueAction;
import com.tonyodev.fetch2.Error;
import com.tonyodev.fetch2.Priority;
import com.tonyodev.fetch2.Status;
import com.tonyodev.fetch2.database.DownloadInfo;
import com.tonyodev.fetch2.database.FetchDatabaseManagerWrapper;
import com.tonyodev.fetch2.util.Defaults;
import com.tonyodev.fetch2core.Extras;
import com.tonyodev.fetchmigrator.fetch1.DatabaseHelper;
import com.tonyodev.fetchmigrator.fetch1.DownloadTransferPair;
import com.tonyodev.fetchmigrator.fetch1.FetchConst;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 数据转换工具类
 *
 * @since 2021-05-24
 */
public class TypeConverters {
    /**
     * 转换数据，从V1版本更新为V2版本
     *
     * @param cursor 数据集
     * @param wrapper 数据库管理器包装类
     * @return 更新后的数据内容
     */
    public static final DownloadTransferPair v1CursorToV2DownloadInfo(
        ResultSet cursor, FetchDatabaseManagerWrapper wrapper) {
        long id = cursor.getLong(DatabaseHelper.INDEX_ID);
        int status = cursor.getInt(DatabaseHelper.INDEX_COLUMN_STATUS);
        String url = cursor.getString(DatabaseHelper.INDEX_COLUMN_URL);
        String file = cursor.getString(DatabaseHelper.INDEX_COLUMN_FILEPATH);
        int error = cursor.getInt(DatabaseHelper.INDEX_COLUMN_ERROR);
        long total = cursor.getLong(DatabaseHelper.INDEX_COLUMN_FILE_SIZE);
        int priority = cursor.getInt(DatabaseHelper.INDEX_COLUMN_PRIORITY);
        long downloaded = cursor.getLong(DatabaseHelper.INDEX_COLUMN_DOWNLOADED_BYTES);
        String headers = cursor.getString(DatabaseHelper.INDEX_COLUMN_HEADERS);

        DownloadInfo downloadInfo = wrapper.getNewDownloadInfoInstance();
        downloadInfo.setId((url.hashCode() * 31) + file.hashCode());
        downloadInfo.setUrl(url);
        downloadInfo.setFile(file);
        downloadInfo.setStatus(getStatusFromV1ForV2(status));
        downloadInfo.setTotal(total);
        downloadInfo.setDownloaded(downloaded);
        downloadInfo.setHeaders(fromHeaderStringToMap(headers));
        downloadInfo.setPriority(getPriorityFromV1ForV2(priority));
        downloadInfo.setError(getErrorFromV1ForV2(error));
        downloadInfo.setEnqueueAction(EnqueueAction.REPLACE_EXISTING);
        downloadInfo.setIdentifier((long) downloadInfo.getId());
        downloadInfo.setDownloadOnEnqueue(Defaults.DEFAULT_DOWNLOAD_ON_ENQUEUE);
        downloadInfo.setExtras(new Extras());
        return new DownloadTransferPair(downloadInfo, id);
    }

    /**
     * 转换状态值的对应值，输入V1版本，输出V2版本对应值
     *
     * @param v1StatusCode V1版本状态值
     * @return 状态值
     */
    public static final Status getStatusFromV1ForV2(int v1StatusCode) {
        switch (v1StatusCode) {
            case FetchConst.STATUS_QUEUED:
                return Status.QUEUED;
            case FetchConst.STATUS_DOWNLOADING:
                return Status.DOWNLOADING;
            case FetchConst.STATUS_DONE:
                return Status.COMPLETED;
            case FetchConst.STATUS_ERROR:
                return Status.FAILED;
            case FetchConst.STATUS_PAUSED:
                return Status.PAUSED;
            case FetchConst.STATUS_REMOVED:
                return Status.REMOVED;
            case FetchConst.STATUS_NOT_QUEUED:
            default:
                return Status.NONE;
        }
    }

    /**
     * 转换优先级的对应值，输入V1版本，输出V2版本对应值
     *
     * @param v1PriorityCode V1版本优先级
     * @return V2优先级
     */
    public static final Priority getPriorityFromV1ForV2(int v1PriorityCode) {
        switch (v1PriorityCode) {
            case FetchConst.PRIORITY_HIGH:
                return Priority.HIGH;
            case FetchConst.PRIORITY_NORMAL:
                return Priority.NORMAL;
            default:
                return Priority.LOW;
        }
    }

    /**
     * 转换错误码的对应值，输入V1版本，输出V2版本对应值
     *
     * @param v1ErrorCode v1版本错误码
     * @return 错误码
     */
    public static final Error getErrorFromV1ForV2(int v1ErrorCode) {
        switch (v1ErrorCode) {
            case FetchConst.ERROR_FILE_NOT_CREATED:
                return Error.FILE_NOT_CREATED;
            case FetchConst.ERROR_CONNECTION_TIMEOUT:
                return Error.CONNECTION_TIMED_OUT;
            case FetchConst.ERROR_UNKNOWN_HOST:
                return Error.UNKNOWN_HOST;
            case FetchConst.ERROR_HTTP_NOT_FOUND:
                return Error.HTTP_NOT_FOUND;
            case FetchConst.ERROR_WRITE_PERMISSION_DENIED:
                return Error.WRITE_PERMISSION_DENIED;
            case FetchConst.ERROR_NO_STORAGE_SPACE:
                return Error.NO_STORAGE_SPACE;
            case FetchConst.ERROR_SERVER_ERROR:
                return Error.EMPTY_RESPONSE_FROM_SERVER;
            case FetchConst.ERROR_REQUEST_ALREADY_EXIST:
                return Error.REQUEST_ALREADY_EXIST;
            case FetchConst.ERROR_ENQUEUE_ERROR:
                return Error.FETCH_DATABASE_ERROR;
            default:
                return Error.UNKNOWN;
        }
    }

    /**
     * 将header字符串转换成map集合
     * 需要考虑JSONObject在转换headerString时的处理方式
     *
     * @param headerString header字符串
     * @return map集合
     */
    public static final Map<String, String> fromHeaderStringToMap(String headerString) {
        Map map = new LinkedHashMap();
        map.put(headerString, headerString);
        return map;
    }
}
